package com.bank.backend.boot.server.service.impl;

import cn.hutool.core.util.StrUtil;
import com.bank.backend.boot.base.constant.ExceptionConstant;
import com.bank.backend.boot.base.enums.AuditEnum;
import com.bank.backend.boot.base.enums.TradeEnum;
import com.bank.backend.boot.base.model.dto.AuditDTO;
import com.bank.backend.boot.base.model.entity.Audit;
import com.bank.backend.boot.base.model.entity.BankUser;
import com.bank.backend.boot.base.model.entity.Trade;
import com.bank.backend.boot.base.model.exception.GlobalException;
import com.bank.backend.boot.base.model.vo.AuditVO;
import com.bank.backend.boot.server.converter.AuditConverter;
import com.bank.backend.boot.server.mapper.AuditMapper;
import com.bank.backend.boot.server.mapper.BankUserMapper;
import com.bank.backend.boot.server.mapper.TradeMapper;
import com.bank.backend.boot.server.service.AuditService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author JiaoWei
* @description 针对表【audit(人工审核)】的数据库操作Service实现
* @createDate 2025-04-26 22:27:46
*/
@Slf4j
@Service
public class AuditServiceImpl extends ServiceImpl<AuditMapper, Audit>
    implements AuditService {

    @Resource
    private AuditMapper auditMapper;

    @Resource
    private AuditConverter auditConverter;

    @Resource
    private BankUserMapper bankUserMapper;

    @Resource
    private TradeMapper tradeMapper;

    @Override
    public Page<AuditVO> queryAuditPage(int current, int pageSize) {
        Page<Audit> page = new Page<>(current, pageSize);
//        QueryWrapper<Audit> wrapper = new QueryWrapper<>();
//        wrapper.eq("buid", Long.parseLong(ThreadLocalUtil.get()));
        Page<Audit> auditPage = auditMapper.selectPage(page, null);
        if (null == auditPage) {
            return null;
        }
        Page<AuditVO> auditVOPage = new Page<>(current, pageSize);
        List<AuditVO> records = new ArrayList<>();
        for (Audit audit : auditPage.getRecords()) {
            BankUser bankUser = bankUserMapper.selectById(audit.getBuid());
            AuditVO auditVO = auditConverter.converter(audit);
            auditVO.setBankNumber(bankUser.getBankNumber());
            records.add(auditVO);
        }
        auditVOPage.setTotal(auditPage.getTotal());
        auditVOPage.setCurrent(auditPage.getCurrent());
        auditVOPage.setSize(auditPage.getSize());
        auditVOPage.setPages(auditPage.getPages());
        auditVOPage.setRecords(records);
        return auditVOPage;
    }

    @Override
    public boolean saveAudit(AuditDTO auditDTO) {
        String bankNumber = auditDTO.getBankNumber();
        if (StrUtil.isEmpty(bankNumber)) {
            throw new GlobalException(ExceptionConstant.BANK_NUMBER_NULL);
        }
        BankUser bankUser = bankUserMapper.getBankUserByBankNumber(bankNumber);
        if (null == bankUser) {
            throw new GlobalException(ExceptionConstant.USER_NOT_EXIST);
        }
        Audit audit = auditConverter.converter(auditDTO);
        audit.setBuid(bankUser.getId());
        audit.setCreateTime(new Date());
        audit.setUpdateTime(new Date());
        if (auditDTO.getResult() == AuditEnum.RESULT_0.getCode()) {
            audit.setFinishTime(null);
        } else {
            audit.setFinishTime(new Date());
            UpdateWrapper<Trade> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", auditDTO.getTid())
                    .set("finish_time", new Date());

            if (auditDTO.getResult() == AuditEnum.RESULT_2.getCode()) {
                updateWrapper.set("risk", TradeEnum.RISK_0.getCode())
                        .set("finish", TradeEnum.Finish_1.getCode());
            } else {
                updateWrapper.set("risk", TradeEnum.RISK_3.getCode())
                        .set("finish", TradeEnum.Finish_2.getCode());
            }
            int ti = tradeMapper.update(updateWrapper);
            if (ti == 0) {
                throw new GlobalException(ExceptionConstant.UPDATE_ERROR);
            }
        }
        int i = auditMapper.insert(audit);
        //TODO 增加账户可疑次数
        return i != 0;
    }
}
